import { _decorator, Component, Label, Node, randomRangeInt, Sprite, SpriteFrame, tween, v3, Vec3 } from 'cc';
import { EasingType } from 'db://assets/Scripts/Framework/Utils/TweenUtil';
import { GameDataManager } from '../GameDataManager';
import Banner from 'db://assets/Scripts/Banner';
import { MainUI } from './MainUI';
import { BundleManager } from 'db://assets/Scripts/Framework/Managers/BundleManager';
import { ProjectEventManager, ProjectEvent } from 'db://assets/Scripts/Framework/Managers/ProjectEventManager';
const { ccclass, property } = _decorator;

@ccclass('GachaUI')
export class GachaUI extends Component {

    @property(Node)
    freeLabel: Node;

    @property(Node)
    videocon: Node;

    @property(Node)
    roll: Node;

    @property(Node)
    mask: Node;

    @property(Sprite)
    rollSprite: Sprite;

    @property([SpriteFrame])
    rollSfs: SpriteFrame[] = [];

    protected onEnable(): void {
        tween(this.node.children[1])
            .to(0.5, { scale: Vec3.ONE }, { easing: EasingType.backOut })
            .start();
        this._Init();
        ProjectEventManager.emit(ProjectEvent.弹出窗口);
    }

    start() {

    }

    update(deltaTime: number) {

    }

    _Init() {
        let gacha = GameDataManager.Instance.getNumberData("IsGacha");
        this.freeLabel.active = gacha == 0;
        this.videocon.active = gacha == 1;
    }

    Back() {
        tween(this.node.children[1])
            .to(0.5, { scale: Vec3.ZERO }, { easing: EasingType.backIn })
            .call(() => { this.node.active = false; })
            .start();
        MainUI.Instance.PlayClick();
    }

    Gacha() {
        let x = this;
        let gacha = GameDataManager.Instance.getNumberData("IsGacha");
        if (gacha) Banner.Instance.ShowVideoAd(() => { x.GachaStart(); });
        else this.GachaStart();
        MainUI.Instance.PlayClick();
    }

    GachaStart() {
        let num = randomRangeInt(0, 100);
        if (num < 20) num = 0;
        else if (num < 30) num = 1;
        else if (num < 35) num = 2;
        else if (num < 55) num = 3;
        else if (num < 65) num = 4;
        else if (num < 70) num = 5;
        else if (num < 90) num = 6;
        else num = 7;
        this.mask.active = true;
        this.rollSprite.spriteFrame = this.rollSfs[0];
        let angle = 45;
        this.roll.setRotationFromEuler(v3(0, 0, angle));
        let times = 0;
        this.schedule(() => {
            angle -= 45;
            this.roll.setRotationFromEuler(v3(0, 0, angle));
            times++;
            if (times == 31) {
                times = 0;
                this.schedule(() => {
                    angle -= 45;
                    this.roll.setRotationFromEuler(v3(0, 0, angle));
                    if (times == num) {
                        this.GachaEnd(num);
                    }
                    else times++;
                }, 0.1, num);
            }
        }, 0.1, 30);
    }

    GachaEnd(roll: number) {
        GameDataManager.Instance.setNumberData("IsGacha", 1);
        this.mask.active = false;
        this.rollSprite.spriteFrame = this.rollSfs[1];
        let type = 0;
        let num = 0;
        switch (roll) {
            case 0:
                type = 0;
                num = 100;
                break;
            case 1:
                type = 1;
                num = 50;
                break;
            case 2:
                type = 2;
                num = 1;
                break;
            case 3:
                type = 0;
                num = 200;
                break;
            case 4:
                type = 1;
                num = 100;
                break;
            case 5:
                type = 2;
                num = 2;
                break;
            case 6:
                type = 0;
                num = 500;
                break;
            case 7:
                type = 1;
                num = 250;
                break;
        }
        let label = MainUI.Instance.finish.children[3].getComponent(Label);
        switch (type) {
            case 0:
                let coin = GameDataManager.Instance.getNumberData("Coin");
                coin += num;
                GameDataManager.Instance.setNumberData("Coin", coin);
                label.string = `金币x${num}`;
                break;
            case 1:
                let diamond = GameDataManager.Instance.getNumberData("Diamond");
                diamond += num;
                GameDataManager.Instance.setNumberData("Diamond", diamond);
                label.string = `钻石x${num}`;
                break;
            case 2:
                let skinPieces = GameDataManager.Instance.getArrayData<number[]>("SkinPieces");
                let type = randomRangeInt(0, skinPieces.length);
                let num2 = randomRangeInt(0, skinPieces[type].length);
                skinPieces[type][num2]++;
                GameDataManager.Instance.setArrayData("SkinPieces", skinPieces);
                let data = MainUI.Instance.items.Adorn[type][num2];
                let sprite = MainUI.Instance.finish.children[2].children[2].children[0].getComponent(Sprite);
                BundleManager.GetBundle("MainPack").load(data.spriteFrame, SpriteFrame, (err, data) => { sprite.spriteFrame = data; });
                label.string = `${data.name}碎片x${num}`;
                break;
        }
        MainUI.Instance._InitToken();
        this._Init();
        for (let i = 0; i < MainUI.Instance.finish.children[2].children.length; i++) {
            const element = MainUI.Instance.finish.children[2].children[i];
            element.active = i == type;
        }
        tween(MainUI.Instance.finish)
            .to(0.5, { scale: Vec3.ONE }, { easing: EasingType.backOut })
            .start();
        this._Init();
        MainUI.Instance._InitRed();
        MainUI.Instance.PlayBonus();
    }
}


